package com.shahe.GirlsDay.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.HttpParams;
import org.apache.http.protocol.HTTP;
import org.json.JSONArray;
import org.json.JSONObject;

import android.util.Log;

public class HttpHelper {

	private static final String TAG = "Httphepler";
	public static final String BASE_URL = "http://192.168.1.110:8080/WebApplication2/webresources";

	// public static final String BASE_URI =
	// "http://118.139.30.129:8080/MyMateServer/webresources";

	public static String resfulClient(String str) {
		HttpClient client = new DefaultHttpClient();
		HttpGet request = new HttpGet(str);

		try {
			Log.i(TAG, str);
			HttpResponse response = client.execute(request);
			InputStream input = response.getEntity().getContent();
			String result = "";
			BufferedReader reader = new BufferedReader(new InputStreamReader(
					input));
			StringBuilder sb = new StringBuilder();
			while ((result = reader.readLine()) != null) {
				sb.append(result);
			}
			Log.i(TAG, sb.toString());

			return sb.toString();
		} catch (Exception e) {
			e.printStackTrace();
			return "Try again";
		}
	}

	public static int postClient(String str, String json) {
		HttpClient client = new DefaultHttpClient();
		HttpPost httpPost = new HttpPost(str);
		httpPost.setHeader("Content-type", "application/json");
		httpPost.setHeader("Accept", "application/json");
		try {
			Log.i(TAG, str);
			StringEntity entityData = new StringEntity(json);			
			httpPost.setEntity(entityData);
			HttpResponse response = client.execute(httpPost);
			int statusCode = response.getStatusLine().getStatusCode();
			return statusCode;

		} catch (Exception e) {
			e.printStackTrace();
		}
		return 0;
	}

	// login
	public static String checkLogin(String sid) {
		String entity = "/entity.userinfo/login/";
		String URL = BASE_URL + entity + sid;
		return resfulClient(URL);
	}

	// getCP
	public static String getCP(String userId) {
		String entity = "/entity.contactperson/getcp/";
		String URL = BASE_URL + entity + userId;
		return resfulClient(URL);
	}

	// checkusername

	public static String checkUsername(String str) {
		String entity = "/entity.userinfo/checkusername/";
		String URL = BASE_URL + entity + str;
		return resfulClient(URL);
	}

	// getNewUserId

	public static String getUserId(String str) {

		String entity = "/entity.userinfo/count";
		String URL = BASE_URL + entity;
		return resfulClient(URL);

	}

	// newcpId

	public static String getCPersonId(String str) {

		String entity = "/entity.contactperson/count";
		String URL = BASE_URL + entity;
		return resfulClient(URL);

	}

	// postUserInfo

	@SuppressWarnings("null")
	public static int postUserInfo(String json) {		
		String entity = "/entity.userinfo/post";
		String URL = BASE_URL + entity;
		return postClient(URL, json);
	}

	// postCP
	@SuppressWarnings("null")
	public static int postCPInfo(String json) {
		String entity = "/entity.contactperson/post";
		String URL = BASE_URL + entity;
		return postClient(URL, json);
	}

/*	// get Basic id
	public static String getBasicId(String str) {
		String entity = "/entity.basicinfo/count";
		String URL = BASE_URL + entity;
		return resfulClient(URL);
	}*/
	
	//get basic by userid
	public static String getBasic(String str) {
		String entity = "/entity.basicinfo/getBasicinfo/";
		String URL = BASE_URL + entity + str;
		return resfulClient(URL);
	}
	
	public static int postBasic(String json) {
		String entity = "/entity.basicinfo/post";
		String URL = BASE_URL + entity;
		return postClient(URL, json);
	}
	

	
}
